#region License
// ResourceViewModel.cs
// 
// Copyright (c) 2012 Xoqal.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace Shammateh.Win.UI.ViewModels
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Xoqal.Data;

    public class ResourceViewModel : ShammatehCrudViewModel<Entities.Resource>
    {
        #region Fields

        private readonly Data.IResourceRepository resourceRepository;
        private string filterFirstName;
        private string filterLastName;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ResourceViewModel" /> class.
        /// </summary>
        /// <param name="unitOfWork">The unit of work.</param>
        /// <param name="resourceRepository">The resource repository.</param>
        public ResourceViewModel(IUnitOfWork unitOfWork, Data.IResourceRepository resourceRepository)
            : base(unitOfWork, resourceRepository)
        {
            this.resourceRepository = resourceRepository;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the view title.
        /// </summary>
        /// <value>
        /// The view title.
        /// </value>
        public override string ViewTitle
        {
            get { return "افراد"; }
        }

        /// <summary>
        /// Gets or sets the first name of the filter.
        /// </summary>
        /// <value>
        /// The first name of the filter.
        /// </value>
        public string FilterFirstName
        {
            get
            {
                return this.filterFirstName;
            }

            set
            {
                if (this.filterFirstName != value)
                {
                    this.filterFirstName = value;
                    this.RaisePropertyChanged(() => this.FilterFirstName);
                    this.UpdateData();
                }
            }
        }

        /// <summary>
        /// Gets or sets the last name of the filter.
        /// </summary>
        /// <value>
        /// The last name of the filter.
        /// </value>
        public string FilterLastName
        {
            get
            {
                return this.filterLastName;
            }

            set
            {
                if (this.filterLastName != value)
                {
                    this.filterLastName = value;
                    this.RaisePropertyChanged(() => this.FilterLastName); 
                    this.UpdateData();
                }
            }
        }

        #endregion

        #region Protected Methods

        /// <summary>
        /// Gets the query.
        /// </summary>
        /// <returns></returns>
        protected override IQueryable<Entities.Resource> GetQuery()
        {
            var query = this.resourceRepository.Query;

            if (!string.IsNullOrWhiteSpace(this.FilterFirstName))
            {
                query = query.Where(p => p.FirstName.ToLower().Contains(this.FilterFirstName.ToLower()));
            }

            if (!string.IsNullOrWhiteSpace(this.FilterLastName))
            {
                query = query.Where(p => p.LastName.ToLower().Contains(this.FilterLastName.ToLower()));
            }

            return query;
        }

        #endregion

        #region Private Methods

        #endregion
    }
}
